import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/page/work/work_page.dart';
import 'package:enter_prise_web/util/MyDio.dart';
import 'package:get/get.dart';

class WorkController extends GetxController {
  final MyDio myDio = MyDio();

  var uid = ''.obs;
  var isSingle = false.obs;

  var number = 0.obs;
  var price = 0.obs;

  @override
  void onInit() {
    super.onInit();
    getWorkTodayRecord(queryDays: FilterType.week);
  }

  Map<String, dynamic> allMsg = {};
  List workTodayRecords = [];

//根据起始日期和结束日期获取记录
  void getWorkTodayRecord({FilterType? queryDays = FilterType.week}) async {
    workTodayRecords.clear();
    allMsg.clear();

    int offset = -7;
    int len = 7;
    switch (queryDays) {
      case FilterType.day:
        offset = -1;
        len = 2;
      case FilterType.week:
        offset = -7;
        len = 8;
        break;
      case FilterType.month:
        offset = -30;
        len = 31;
        break;
      case FilterType.year:
        offset = -365;
        len = 367;
        break;
      default:
        break;
    }
    String url = '';
    if (isSingle.value) {
      url = "/workRecord/selectAllAdmin?uid=${uid.value}";
    } else {
      url = "/workRecord/selectToday";
    }
    var response = await myDio.requestData(url, method: 'GET', params: {
      "offset": offset,
      "len": len,
    });
    price.value = response.data["total"];
    allMsg = response.data;

    number.value = allMsg.length;

    if (allMsg["works"].length == 0) {
      update(['work_list']);
      return;
    }

    if (queryDays == FilterType.illegal) {
      allMsg['works'].asMap().forEach((key, value) {
        if (allMsg['works'][key]["valid"] == 0) {
          workTodayRecords.add(allMsg['works'][key]);
        }
      });
    } else {
      workTodayRecords = allMsg['works'];
    }
    update(['work_list']);

    afterRefresh();
  }

  void updateData(FilterType? queryDays) {
    getWorkTodayRecord(queryDays: queryDays);
  }
}
